<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {
	private $admin_db,$platform;
	public function __construct(){  
        parent::__construct();
		$this->admin_db = $this->load->database('admin',true);
		$this->load->config('config');
		$this->platform	=	$this->config->item('platform');
    }

	public function index()
	{	
	    $this->load->view('login/index',array());
	}

	public function doLogin()
	{	
		$post = $this->input->post();
		if(empty($post)){
			jsonReturn(-1,"提交失败，请重新再试！");
		}

		$username = $post['username'];
		$password = $post['password'];
		if(empty($username)){
			jsonReturn(-1,"用户名不能为空！");
		}
		if(empty($password)){
			jsonReturn(-1,"密码不能为空！");
		}

		$this->load->library('Admin_Class');
		$data_arr	=	$this->admin_class->checkAdmin($username,$password);
		if(empty($data_arr)){
			jsonReturn(-1,"用户名或密码输出错误！");
		}
		//链接redis

		$this->load->driver('cache');
		$this->cache->redis->is_supported();
		$redis	=	$this->cache->redis->getClient();
		$key	=	'platform['.$this->platform.']_admin_online|'.$data_arr['admin_id'];
		
		//账号可重复登录限制
		$datetime	=	date("Y-m-d H:i:s");
		$expire		=	"43200";
		if($data_arr['repeat_login']==0){
			$redis->set($key,$datetime);
			$redis->expire($key,$expire);
		}else{
			$redis->delete($key);
		}
		$this->input->set_cookie("platform[".$this->platform."]_admin_login",$data_arr['admin_id'],$expire);
		$this->admin_class->addLoginLog($data_arr);
		jsonReturn('0','登录成功！');
	}

	function out(){
		$this->input->set_cookie("platform[".$this->platform."]_admin_login",'',0);
		echo <<<EOD
            <script>
				//alert('安全退出成功！');
				window.location.href='/';
            </script>
EOD;
	}
}
?>